package com.sxj.PDMS.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxj.PDMS.common.ErrorCode;
import com.sxj.PDMS.exception.BusinessException;
import com.sxj.PDMS.mapper.FullPartyMemberInformationListMapper;
import com.sxj.PDMS.model.dto.admin.PartyMemberPunishmentsRequest;
import com.sxj.PDMS.model.entity.FullPartyMemberInformationList;
import com.sxj.PDMS.model.entity.PartyMemberPunishments;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.PartyMemberPunishmentsService;
import com.sxj.PDMS.mapper.PartyMemberPunishmentsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.sxj.PDMS.utils.VerifyUtil.formatDate;

/**
* @author 503c'j
* @description 针对表【party_member_punishments(党员惩罚表)】的数据库操作Service实现
* @createDate 2024-06-16 17:28:12
*/
@Service
public class PartyMemberPunishmentsServiceImpl extends ServiceImpl<PartyMemberPunishmentsMapper, PartyMemberPunishments>
    implements PartyMemberPunishmentsService{
    @Autowired
    private PartyMemberPunishmentsMapper partyMemberPunishmentsMapper;
    @Autowired
    private FullPartyMemberInformationListMapper fullPartyMemberInformationListMapper;

    @Override
    public Boolean punishmentsUpload(PartyMemberPunishmentsRequest partyMemberPunishmentsRequest) throws ParseException {
        FullPartyMemberInformationList fullPartyMemberInformationList = fullPartyMemberInformationListMapper.selectById(partyMemberPunishmentsRequest.getPartyMemberNumber());
        if(fullPartyMemberInformationList == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, partyMemberPunishmentsRequest.getPartyMemberNumber() + "的党员不存在");
        }
        PartyMemberPunishments partyMemberPunishments = new PartyMemberPunishments();
        BeanUtils.copyProperties(partyMemberPunishmentsRequest, partyMemberPunishments);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        partyMemberPunishments.setPunishmentDate(formatDate(currentTime));
        boolean saved = this.save(partyMemberPunishments);
        return saved;
    }
    @Override
    public PagingQueryResponse<PartyMemberPunishments> punishmentsSelect(Integer page, Integer pageSize, PartyMemberPunishments partyMemberPunishments) {
        Integer start = (page - 1) * pageSize;
        String punishmentContent = partyMemberPunishments.getPunishmentContent();
        Long partyMemberNumber = partyMemberPunishments.getPartyMemberNumber();
        Date punishmentDate = partyMemberPunishments.getPunishmentDate();

        List<PartyMemberPunishments> PartyMemberPunishmentsList = partyMemberPunishmentsMapper.selectByContentOrMemberNumberOrDate(start, pageSize, punishmentContent,partyMemberNumber, punishmentDate);
        Long total = partyMemberPunishmentsMapper.selectTotalByContentOrMemberNumberOrDate( punishmentContent,partyMemberNumber, punishmentDate);

        PagingQueryResponse<PartyMemberPunishments> pagingQueryResponse = new PagingQueryResponse<>();
        pagingQueryResponse.setRows(PartyMemberPunishmentsList);
        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

    @Override
    public PagingQueryResponse<PartyMemberPunishments> selectAll(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<PartyMemberPunishments> partyMemberAwardsList = partyMemberPunishmentsMapper.selectAll(start, pageSize);
        Long total = partyMemberPunishmentsMapper.selectTotal();
        PagingQueryResponse<PartyMemberPunishments> pagingQueryResponse = new PagingQueryResponse<>();

        pagingQueryResponse.setRows(partyMemberAwardsList);

        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }
}




